package com.scms.secondclass.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.scms.common.core.domain.entity.SysUser;
import com.scms.common.utils.DateUtils;
import com.scms.common.utils.SecurityUtils;
import com.scms.common.utils.StringUtils;
import com.scms.secondclass.domain.ActivityCategory;
import com.scms.secondclass.mapper.ActivityCategoryMapper;
import com.scms.secondclass.service.IActivityCategoryService;

/**
 * 活动分类Service业务层处理
 * 
 * @author scms
 */
@Service
public class ActivityCategoryServiceImpl implements IActivityCategoryService 
{
    @Autowired
    private ActivityCategoryMapper activityCategoryMapper;

    /**
     * 查询活动分类
     * 
     * @param categoryId 活动分类ID
     * @return 活动分类
     */
    @Override
    public ActivityCategory selectActivityCategoryByCategoryId(Long categoryId)
    {
        return activityCategoryMapper.selectActivityCategoryByCategoryId(categoryId);
    }

    /**
     * 查询活动分类列表
     * 
     * @param activityCategory 活动分类
     * @return 活动分类
     */
    @Override
    public List<ActivityCategory> selectActivityCategoryList(ActivityCategory activityCategory)
    {
        return activityCategoryMapper.selectActivityCategoryList(activityCategory);
    }

    /**
     * 构建前端所需要的树形结构
     * 
     * @param activityCategories 活动分类列表
     * @return 树结构列表
     */
    @Override
    public List<ActivityCategory> buildActivityCategoryTree(List<ActivityCategory> activityCategories)
    {
        List<ActivityCategory> returnList = new ArrayList<ActivityCategory>();
        List<Long> tempList = activityCategories.stream().map(ActivityCategory::getCategoryId).collect(Collectors.toList());
        for (ActivityCategory activityCategory : activityCategories)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (activityCategory.getParentId() == null || !tempList.contains(activityCategory.getParentId()))
            {
                recursionFn(activityCategories, activityCategory);
                returnList.add(activityCategory);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = activityCategories;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<ActivityCategory> list, ActivityCategory t)
    {
        // 得到子节点列表
        List<ActivityCategory> childList = getChildList(list, t);
        t.setChildren(childList);
        for (ActivityCategory tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<ActivityCategory> getChildList(List<ActivityCategory> list, ActivityCategory t)
    {
        List<ActivityCategory> tlist = new ArrayList<ActivityCategory>();
        Iterator<ActivityCategory> it = list.iterator();
        while (it.hasNext())
        {
            ActivityCategory n = it.next();
            if (n.getParentId() != null && t.getCategoryId() != null && n.getParentId().longValue() == t.getCategoryId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<ActivityCategory> list, ActivityCategory t)
    {
        return getChildList(list, t).size() > 0;
    }

    /**
     * 新增活动分类
     * 
     * @param activityCategory 活动分类
     * @return 结果
     */
    @Override
    public int insertActivityCategory(ActivityCategory activityCategory)
    {
        activityCategory.setCreateBy(SecurityUtils.getUsername());
        activityCategory.setCreateTime(DateUtils.getNowDate());
        return activityCategoryMapper.insertActivityCategory(activityCategory);
    }

    /**
     * 修改活动分类
     * 
     * @param activityCategory 活动分类
     * @return 结果
     */
    @Override
    public int updateActivityCategory(ActivityCategory activityCategory)
    {
        activityCategory.setUpdateBy(SecurityUtils.getUsername());
        activityCategory.setUpdateTime(DateUtils.getNowDate());
        return activityCategoryMapper.updateActivityCategory(activityCategory);
    }

    /**
     * 批量删除活动分类
     * 
     * @param categoryIds 需要删除的活动分类ID
     * @return 结果
     */
    @Override
    public int deleteActivityCategoryByCategoryIds(Long[] categoryIds)
    {
        return activityCategoryMapper.deleteActivityCategoryByCategoryIds(categoryIds);
    }

    /**
     * 删除活动分类信息
     * 
     * @param categoryId 活动分类ID
     * @return 结果
     */
    @Override
    public int deleteActivityCategoryByCategoryId(Long categoryId)
    {
        // 判断是否存在子分类
        if (activityCategoryMapper.hasChildByCategoryId(categoryId) > 0)
        {
            throw new RuntimeException("存在子分类,不允许删除");
        }
        // 判断分类是否已被使用
        if (activityCategoryMapper.checkCategoryUsed(categoryId) > 0)
        {
            throw new RuntimeException("分类已被活动使用,不允许删除");
        }
        return activityCategoryMapper.deleteActivityCategoryByCategoryId(categoryId);
    }
} 